<template>
  <div id="admin-manage" class="page-wrapper">
    <div class="selector-wrapper main-card wrapper">
      <div class="main-card">
        <GroupSelector :selectorData="selectorData" />
      </div>
      <el-button type="primary" @click="handleSubmitClick">获取数据</el-button>
    </div>

    <div class="wrapper main-card">
      <el-table
        :cell-style="{ textAlign: 'center' }"
        :header-cell-style="{ 'text-align': 'center' }"
        ref="singleTable"
        :data="pageData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column property="SS_MC" label="房间号"></el-table-column>
        <el-table-column
          property="meterNumber"
          label="电表编号"
        ></el-table-column>
        <el-table-column
          property="totalMoney"
          label="账号总额(元)"
        ></el-table-column>
        <el-table-column
          property="depositMoney"
          label="存款余额(元)"
        ></el-table-column>
        <el-table-column
          property="subsidyMoney"
          label="补助余额(元)"
        ></el-table-column>
        <el-table-column
          property="meterRead"
          label="电表读数(度)"
        ></el-table-column>
        <el-table-column prop="buildingId" label="收费模板"></el-table-column>
        <el-table-column prop="buildingId" label="补助模板"></el-table-column>
        <el-table-column
          property="lastRead"
          label="上次电表读数(度)"
        ></el-table-column>
        <el-table-column label="电器状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
              @change="handleSwitchChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column property="power" label="使用功率"></el-table-column>
        <el-table-column property="date" label="用表日期" width="170px">
          <template slot-scope="scope">
            {{ formatDate(scope.row.date) }}
          </template>
        </el-table-column>
        <el-table-column
          property="master_id"
          label="主控编号"
        ></el-table-column>
        <el-table-column
          property="limitPower"
          label="限制功率"
        ></el-table-column>
        <el-table-column
          fixed="right"
          property="button"
          label="操作"
          width="180px"
        >
          <template slot-scope="scope">
            <el-button>抄表</el-button>
            <el-button @click="modify(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      title="参数详情"
      :visible.sync="dialogVisible"
      width="800px"
      height="800px"
    >
      <el-tabs type="border-card">
        <el-form class="form" ref="form" :model="modifydata">
          <div class="form-item-wrapper">
            <el-form-item label="设备id" required prop="id">
              <el-input disabled v-model.trim="modifydata.id"></el-input>
            </el-form-item>
            <el-form-item label="开关状态" required prop="state">
              <el-switch
                v-model.trim="modifydata.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </div>
          <div class="form-item-wrapper">
            <el-form-item label="限制功率" required prop="limitPower">
              <el-input
                v-model.trim="modifydata.limitPower"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
            <el-form-item label="使用功率" required prop="power">
              <el-input
                v-model.trim="modifydata.power"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </div>
        </el-form>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import GroupSelector from './components/GroupSelector'
import { upmeter, getmeter } from '@/api/meter'

export default {
  name: 'RoomInfo',
  components: {
    GroupSelector
  },
  data() {
    return {
      currentPage: 1,
      pageData: [],
      total: '',
      pageSize: 10,
      dialogVisible: false,
      selectorData: {
        buildingId: null,
        floorId: null,
        roomId: null
      },
      modifydata: {},
      tableData: []
    }
  },

  methods: {
    queryByPage() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = this.currentPage * this.pageSize
      return this.tableData.slice(start, end)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageData = this.queryByPage()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.pageData = this.queryByPage()
    },
    formatDate(isoString) {
      const date = new Date(isoString)
      const options = {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      }
      return date.toLocaleString('zh-CN', options)
    },
    handleSwitchChange(row) {
      upmeter(row).then(res => {
        if (res.data.result.state === '1') {
          this.$message.success('开关已打开')
        } else {
          this.$message.warning('开关已关闭')
        }
      })
    },
    handleSubmitClick() {
      console.log(this.selectorData)

      getmeter(this.selectorData).then(res => {
        console.log(res)

        if (res.success == true) {
          this.tableData = res.data.meter
          this.pageData = this.queryByPage()
          this.total = res.data.meter.length
        }
      })
    },
    modify(row) {
      this.modifydata = row
      this.dialogVisible = true
    },
    handleSubmit() {
      upmeter(this.modifydata).then(res => {
        if (res.success == true) {
          this.$message.success('修改成功')
          this.dialogVisible = false
        }
      })
    }
  },

  mounted() {
    // 初始化时获取数据
    // this.handleSubmitClick()
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  margin: 40px 0;
}

.selector-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.list-wrapper {
  margin-top: 40px;
}

.el-form-item--small.el-form-item {
  margin-bottom: 0px;
}

.form-item-wrapper {
  display: flex;
  justify-content: space-around;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
